package userInterface;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;

import controller.Controller;

import domain.Movie;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.List;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.swingbinding.SwingBindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;

public class SurveyFrame extends JFrame {

	private JPanel contentPane;
	private List<Movie> movieList;
	
	private Controller controller;
	private JList list;


	/**
	 * Create the frame.
	 */
	public SurveyFrame(final Controller controller) {
		this.controller = controller;
		
		try {
			movieList = controller.getSurveyMovies();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 443);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 414, 73);
		contentPane.add(panel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(20, 95, 404, 299);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(40, 5, 325, 204);
		panel_1.add(scrollPane);
		
		list = new JList();
		scrollPane.add(list);
		scrollPane.setViewportView(list);
		
		JButton Rate = new JButton("Rate");
		Rate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Movie movie = movieList.get(list.getSelectedIndex());
				RateDialog rDialog = new RateDialog(movie, controller);
				rDialog.setVisible(true);
				
			}
		});
		Rate.setBounds(40, 242, 89, 23);
		panel_1.add(Rate);
		
		JButton btnDone = new JButton("Done");
		btnDone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginFrame logFrame = new LoginFrame();
				setVisible(false);
				logFrame.setVisible(true);
			}
		});
		btnDone.setBounds(276, 242, 89, 23);
		panel_1.add(btnDone);
		initDataBindings();
		
	}
	protected void initDataBindings() {
		JListBinding<Movie, List<Movie>, JList> jListBinding = SwingBindings.createJListBinding(UpdateStrategy.READ, movieList, list);
		//
		BeanProperty<Movie, String> movieBeanProperty = BeanProperty.create("movieDescription.name");
		jListBinding.setDetailBinding(movieBeanProperty);
		//
		jListBinding.bind();
	}
}
